var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;



layui.use(["excel","selectInput"], function () {
    let layer = layui.layer,
        excel = layui.excel
        ,selectInput = layui.selectInput;

    lay('.starttime').each(function(){
        laydate.render({
            elem: this
        });
    });

    layer.ready(function () {
        // setCustomerInfo("");
        getxmselect();
        function getxmselect() {
            return  xmSelect.render({
                el: '#demo5',
                name:'supplierCode',
                filterable: true,
                remoteSearch: true,
                height: 'auto',
                paging: true,
                pageSize: 10,
                radio: true,
                clickClose: true,
                model: {
                    icon: 'hidden',
                    label: {
                        type: 'text'
                    }
                },
                remoteMethod: function(val, cb, show){
                    //这里如果val为空, 则不触发搜索
                    if(!val){
                        return cb([]);
                    }
                    $.ajax({
                        url: "basicInformation/getCustomerInfo?info=" + val,
                        async: false,
                        success: function (res) {
                            if (res.status == 200) {
                                for (let i in res.data.customerInfo) {
                                    res.data.customerInfo[i]["value"] = res.data.customerInfo[i].code;
                                }
                                return cb(res.data.customerInfo);
                            }
                        }
                    })
                },
            })
        }
    });

    function setCustomerInfo(initData) {
        //客户名称
        let ins = selectInput.render({
            elem: '#customerSelect',
            name: 'customerSelect',
            initValue: initData,
            hasSelectIcon: true,
            placeholder: '请输入名称',
            data: [],
        })
        //监听输入
        ins.on('itemInput(customerSelect)', function (obj) {
            let tempData = [];
            $.ajax({
                url: "basicInformation/getCustomerInfo?info=" + obj.value,
                async: false,
                success: function (res) {
                    if (res.status == 200) {
                        customerInfo = res.data.customerInfo;
                        for (let i in customerInfo) {
                            customerInfo[i]["value"] = customerInfo[i].code;
                            tempData.push(customerInfo[i]);
                        }
                    }
                }
            })
            ins.addSelect(tempData)
        })

        // 监听select 选择事件
        ins.on('itemSelect(customerSelect)', function (obj) {
            $("#customerCode").val(obj.data);
            $("#customerName").val(obj.textContent);
        });
    }



    form.render();//表单刷新
    let initData = [];
    table.render({
        elem: '#billing1Select'
        ,data:initData
        ,where:{
            sourceType:2
        }
        ,toolbar: '#billing1Toolbar'
        ,totalRow: true
        ,cols: [[
            {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'}
            , {field: 'purchaseOrderCode', title: '采购单号', sort: true, width: 200, align: 'center'}
            , {field: 'contractCode', title: '外销合同号', sort: true, width: 120, align: 'center'}
            , {field: 'productCode', title: '产品编码', sort: true, width: 120, align: 'center'}
            , {field: 'specificationCode', title: '规格编码', sort: true, width: 120, align: 'center'}
            , {field:'number', title:'开票数量',sort:true,width: 120, align: 'center', totalRow: true}
            , {field:'unitPrice', title:'开票单价',sort:true,width: 120, align: 'center', totalRow: true}
            , {field: 'subtotal', title: '开票金额小计', sort: true, width: 120, align: 'center', totalRow: true}
            , {field:'supplierName', title:'客户名称',sort:true,width: 120, align: 'center'}
            , {field:'currencyName', title:'币种',sort:true,width: 120, align: 'center'}
            , {field:'createDateStr', title:'创建日期',sort:true,width: 120, align: 'center'}
            , {field:'billingDateStr', title:'发票收到日期',sort:true,width: 150, align: 'center'}
            , {field: 'goodsNum', title: '外销产品数量', sort: true, width: 120, align: 'center', totalRow: true}
            , {field: 'amountInvoiced', title: '需开票金额', sort: true, width: 120, align: 'center', totalRow: true}
            , {field: 'notAmountInvoiced', title: '未开票金额', sort: true, width: 120, align: 'center', totalRow: true}
            , {field: 'productType', title: '产品类型', sort: true, width: 120, align: 'center'}
        ]]
        ,id: 'maininformationtableid'
        ,height: $(document).height() - $('#billing1Select').offset().top - 75,
        //开启分页
        page: true,
        limits: [300,600,900],
    });


//搜索提交
    form.on('submit(submitform)', function(data){
        table.reload('maininformationtableid', {
            page: {
                limit:300,
                curr: 1
            },
            url:'billing/billingStatistics1ListByPage',
            where:  data.field
        }, 'data');
        return false;
    });

    form.on('submit(billingExcel)', function (data) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: data.field,
            url:'billing/billingStatistics1Export',
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 120,
                        'AJ': 120
                    }, 120);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '客户开票统计.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });

});

